package com.yxuan.springboot.constant.config;


import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner;
import com.wechat.pay.contrib.apache.httpclient.auth.Verifier;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Credentials;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Validator;
import com.wechat.pay.contrib.apache.httpclient.cert.CertificatesManager;
import com.wechat.pay.contrib.apache.httpclient.exception.HttpCodeException;
import com.wechat.pay.contrib.apache.httpclient.exception.NotFoundException;
import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
import com.yxuan.springboot.CustomizedException.DefaultException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.impl.client.CloseableHttpClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.PrivateKey;

/*
* 公众号支付配置
*
* */
@Slf4j
@Configuration
@Data
public class WxPayConfigForAC {


    // APPID
    private String appid = "wx192f019efa82b221";

    // 商户API证书序列号
    private String mchSerialNo = "69EED795F0A1B26F35B8A72C498F7609248EA256";

    // 商户私钥文件路径
//    private String privateKeyPath = "E:\\项目开发\\教育平台\\微信支付证书\\apiclient_key.pem";
    private String privateKeyPath = "cert/apiclient_key.pem";
//    private String privateKeyPath = "C:/智创云链/业务线/星辰智考/1677335562_20240519_cert/apiclient_key.pem";
//    private String privateKeyPath = "D:/plugins/apiclient_key.pem";
    // 商户号
    private String mchId = "1677335562";

    // APIv3密钥
    private String key = "8yG5BM2mWyYhQqN27RdhCmvqwWcgYZMk";

    // 微信服务器地址
    private String domain = "https://api.mch.weixin.qq.com";

    // 接收结果通知地址
    private String notifyDomain = "https://xc1.haoxnet.com:8098/part1";

    /**
     * 获取商户的私钥文件
     *
     * @param filename 证书地址
     * @return 私钥文件*/



    public PrivateKey getPrivateKeyAC(String filename) throws DefaultException {
        try {
            return PemUtil.loadPrivateKey(new FileInputStream(filename));
        } catch (FileNotFoundException e) {
            throw new DefaultException("私钥文件不存在");
        }
    }


    /**
     * 获取签名验证器*/
    @Bean(name = "verifierAC")
    public Verifier getVerifierAC() throws DefaultException {
        // 获取商户私钥
        log.info("获取商户私钥privateKeyPath:"+privateKeyPath);
        final PrivateKey privateKey = getPrivateKeyAC(privateKeyPath);

        // 私钥签名对象
        log.info("mchSerialNo:"+mchSerialNo);
        PrivateKeySigner privateKeySigner = new PrivateKeySigner(mchSerialNo, privateKey);

        // 身份认证对象
        log.info("mchId:"+mchId);
        WechatPay2Credentials wechatPay2Credentials = new WechatPay2Credentials(mchId, privateKeySigner);

        // 获取证书管理器实例
        CertificatesManager certificatesManager = CertificatesManager.getInstance();
        log.info("获取证书管理器实例成功");
        try {
            // 向证书管理器增加需要自动更新平台证书的商户信息
            log.info("向证书管理器增加需要自动更新平台证书的商户信息");
            log.info(key.getBytes(StandardCharsets.UTF_8).toString());
            log.info(wechatPay2Credentials.toString());
            certificatesManager.putMerchant(mchId, wechatPay2Credentials, key.getBytes(StandardCharsets.UTF_8));
            log.info("向证书管理器增加需要自动更新平台证书的商户信息成功");
        } catch (IOException | GeneralSecurityException | HttpCodeException e) {
            log.info("向证书管理器增加需要自动更新平台证书的商户信息失败");
            e.printStackTrace();
        }

        try {
            log.info("certificatesManager.getVerifier(mchId)");
            return certificatesManager.getVerifier(mchId);
        } catch (NotFoundException e) {
            e.printStackTrace();
            log.info("获取签名验证器失败");
            throw new DefaultException("获取签名验证器失败");
        }
    }


    /**
     * 获取微信支付的远程请求对象
     *
     * @return Http请求对象*/
    @Bean(name = "wxPayClientAC")
    public CloseableHttpClient getWxPayClientAC() throws DefaultException {

        // 获取签名验证器
        Verifier verifier = getVerifierAC();

        // 获取商户私钥
        final PrivateKey privateKey = getPrivateKeyAC(privateKeyPath);

        WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create().withMerchant(mchId, mchSerialNo, privateKey)
                .withValidator(new WechatPay2Validator(verifier));

        return builder.build();
    }


}
